package com.jy.novel.controller.system;

import com.jy.novel.common.CommonResult;
import com.jy.novel.common.Fields;
import com.jy.novel.controller.base.BaseController;
import com.jy.novel.pojo.system.UserInfo;
import com.jy.novel.service.system.UserService;
import com.jy.novel.utils.Captcha;
import com.jy.novel.utils.EncryptUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Created by jihb on 4/12/16.
 */
@Controller
public class LoginController extends BaseController {

    @Autowired
    UserService userService;

    @RequestMapping(value = {"/","/index"})
    public String index(){
        return "system/index";
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "system/login";
    }

    @RequestMapping(value = "/loginout",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult loginOut(HttpSession session){
        session.removeAttribute(Fields.LOGIN_USER_INFO);
        return CommonResult.returnBoolWrap(true,"注销成功");
    }
    /**
     * 获取验证码
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value = "/vcode",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult veriCode(HttpServletResponse response, HttpSession session){

        Captcha captcha = new Captcha();
        session.setAttribute(Fields.LOGIN_VCODE,captcha.getCaptchaStr());
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            ByteArrayInputStream inputStream = captcha.getImage();
            int len = 0;
            byte[] byt = new byte[1024];
            while ((len =inputStream.read(byt)) > 0){
                outputStream.write(byt);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult doLogin(
            @RequestParam(value = "loginName")String loginName,
            @RequestParam(value = "loginPass")String loginPass,
            @RequestParam(value = "vcode")String vcode,
            HttpSession session
    ){
        String code = (String) session.getAttribute(Fields.LOGIN_VCODE);
        if (!vcode.equalsIgnoreCase(code)){
            return returnFailure("验证码错误");
        }
        if (StringUtils.isBlank(loginName)){
            return returnFailure("请填写账号");
        }
        if (StringUtils.isBlank(loginPass)){
            return returnFailure("请输入密码");
        }
        UserInfo userInfo = userService.getUserInfo(loginName);
        if (userInfo==null){
            return returnFailure("账号不存在");
        }
        if (!userInfo.getLoginPass().equals(EncryptUtils.encryptMd5(loginPass))){
            return returnFailure("密码错误");
        }
        //登录成功
        session.removeAttribute(Fields.LOGIN_USER_INFO);
        session.setAttribute(Fields.LOGIN_USER_INFO,userInfo);
        return returnBoolWrap(true,"登录成功");
    }
}
